package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你一个二叉树，请你返回其按 层序遍历 得到的节点值。 （即逐层地，从左到右访问所有节点）。
 */
public class T102SequenceTraversalBinaryTree {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        level(root, 0, result);
        return result;
    }

    private void level(TreeNode node,int level,List<List<Integer>> lists){
        if(node == null){
            return ;
        }
        if(lists.size()<=level){
            lists.add(new ArrayList<>());
        }
        System.out.println("level "+ level+" val "+node.val);
        lists.get(level).add(node.val);
        level++;
        level(node.left, level, lists);
        level(node.right, level, lists);

    }

    public static void main(String[] args) {
        TreeNode rootNode = new TreeNode(1);
        constructTreeFactory(rootNode, 2, 2);
        constructTreeFactory(rootNode.left, 3, 4);
        constructTreeFactory(rootNode.right, 4, 3);

        T102SequenceTraversalBinaryTree symmetricBinaryTree = new T102SequenceTraversalBinaryTree();
        List<List<Integer>> lists = symmetricBinaryTree.levelOrder(rootNode);
        System.out.println(lists);

    }

    public static void constructTreeFactory(TreeNode rootNode,int left,int right){
        TreeNode seconedNodeLeft = new TreeNode(left);
        TreeNode seconedNodeRight = new TreeNode(right);
        rootNode.left = seconedNodeLeft;
        rootNode.right = seconedNodeRight;
    }


}
